package com.example.aircraftwar.application.game;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.example.aircraftwar.application.MainActivity;
import com.example.aircraftwar.application.loginorregister.LoginOrRegisterActivity;
import com.example.aircraftwar.application.network.NetConn;
import com.example.aircraftwar.scorelist.Score;
import com.example.aircraftwar.scorelist.ScoreDaoImpl;
import com.example.aircraftwar.scorelist.ScoreRankActivity;
import com.example.aircraftwar.startgame.StartGameActivity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class GameActivity extends AppCompatActivity {

    private Intent intent;
    public static int enemyScore;
    public static boolean enemygameover;
    public static Score enemy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(MainActivity.game);

        if (MainActivity.isonline) {
            new Thread(ready).start();
        }
    }

    Runnable ready = () -> {
        String reply;
        try {
                while ((reply = NetConn.reader.readLine()) != null) {
                    System.out.println(reply);
                    JSONObject jsonObject = JSONObject.fromObject(reply);
                    String msg = jsonObject.getString("msg");
                    String username = jsonObject.getString("username");
                    int score = jsonObject.getInt("score");
                    boolean gameoverflag = jsonObject.getBoolean("gameoverflag");
                    if (msg.equals("game")) {
                        if (!Objects.equals(username, LoginOrRegisterActivity.userName)) {
                            enemygameover = gameoverflag;
                            System.out.println(enemygameover);
                            enemyScore = score;
                            System.out.println("对方得分为：" + score);
                        }
                    } else if (msg.equals("over")) {
                        if (!Objects.equals(username, LoginOrRegisterActivity.userName)) {
                            enemy = new Score(username,score,setTime());
                            intent = new Intent(GameActivity.this, ScoreRankActivity.class);
                            startActivity(intent);
                        }
                    }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    private JSONObject initJson(String userName, String pattern) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", userName);
        jsonObject.put("score", Game.score);
        jsonObject.put("gameoverflag", Game.gameOverFlag);
        return jsonObject;
    }

    public String setTime(){
        Date time = new Date();
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        String currentTime = format.format(time);
        return currentTime;
    }

    void exitAlert() {
        AlertDialog.Builder bld = new AlertDialog.Builder(this);
        bld.setMessage("确认不再玩会吗？");
        bld.setTitle("温馨提示：");
        bld.setPositiveButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int swich) {
                // TODO Auto-generated method stub
                finish();
                Game.gameOverFlag = false;
                Game.score = 0;
                enemyScore = 0;
                intent = new Intent(GameActivity.this, StartGameActivity.class);
                startActivity(intent);
            }
        });
        bld.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int swich) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        bld.create().show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //exit();
            exitAlert();
            return false;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }
    }

}